class YBListLoad {
  // 构造方法
  constructor(modelClass, otherData = {}) {
    /**
     * 当前分页
     * @type {number}
     */
    this.page = 1
    /**
     * 数据数组
     * @type {Array}
     */
    this.dataArr = []
    /**
     * 其他数据（会传送的加载的对象）
     */
    this.otherData = otherData
    /**
     * 模型类
     */
    this.modelClass = modelClass
    /**
     * 是否加载完成
     * @type {boolean}
     */
    this.finish = false
    /**
     * 当前加载状态
     * @type {boolean}
     */
    this.loading = false
  }

  /**
   * 加载数据
   * @param loadName 加载的列表字段名称
   * @returns {Promise<any>}
   */
  loadData(loadName = 'loadModels') {
    return new Promise((resolve, reject) => {
      if (!this.loading && !this.finish) {
        this.loading = true
        this.modelClass[loadName](this.page, this.otherData).then((dataArr) => {
          this.page += 1
          this.finish = dataArr.length < 20
          this.dataArr = this.dataArr.concat(dataArr)
          this.loading = false
          resolve(this.dataArr)
        }).catch((err) => {
          this.loading = false
          const p = {
            load: true,
            err: err
          }
          reject(p)
        })
      } else {
        const p = { load: true }
        reject(p)
      }
    })
  }

  /**
   * 清除数据
   */
  clearData() {
    this.page = 1
    this.dataArr = []
    this.loading = false
    this.finish = false
  }
}

export default YBListLoad
